import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:loxen/utils/string_res.dart';

class SplashPage extends StatefulWidget {
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  final List<String> list = ["index1.png", "index2.png", "index3.png"];
  final SwiperController _controller = SwiperController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Swiper(
          onIndexChanged: (index) {
            if(index == list.length-1) {
              Future.delayed(Duration(seconds: 3),(){
                Navigator.of(context).pop();
              });
            }
          },
//          physics: const NeverScrollableScrollPhysics(),
          itemBuilder: (BuildContext context, int index) {
            return Container(
              child: Image.asset(
                "images/${list[index]}",
                fit: BoxFit.cover,
                gaplessPlayback: true,
              ),
            );
          },
          // indicatorLayout: PageIndicatorLayout.SLIDE,
          loop: false,
          autoplay: true,
          itemCount: list.length,
          pagination: SwiperPagination(builder: _MySwiperPlugin()),
//          controller: _controller,
        ),
        Positioned(
          right: 40.w,
          top: 58.h,
          child: SizedBox(
              width: 120.w,
              height: 56.h,
              child: FlatButton(
                color: Color(0x30000000),
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: Text(
                  StringResources.tiao_guo,
                  maxLines: 1,
                  style: TextStyle(color: Colors.white, fontSize: 24.sp),
                ),
              )),
        )
      ],
    );
  }
}

class _MySwiperPlugin extends SwiperPlugin {
  @override
  Widget build(BuildContext context, SwiperPluginConfig config) {
    double sizeW = 24.w;
    double sizeH = 24.w;
    double leftW = 37.w;
    double bottomH = 86.h;

    List<Widget> list = [];
    int itemCount = config.itemCount;
    int activeIndex = config.activeIndex;
    for (int i = 0; i < itemCount; ++i) {
      bool active = i == activeIndex;
      list.add(Container(
        width: sizeW,
        height: sizeH,
        margin: EdgeInsets.only(left: leftW, bottom: bottomH),
        decoration: BoxDecoration(
            color: active ? Color(0xFF0CABCB) : Color(0x00FFFFFF),
            borderRadius: BorderRadius.all(Radius.circular(sizeW)),
            border: Border.all(
                color: active ? Color(0x00FFFFFF) : Color(0xFF0CABCB),
                width: 1)),
      ));
    }

    return new Row(
      mainAxisSize: MainAxisSize.min,
      children: list,
    );
  }
}
